#include <iostream>
#include <QJsonArray>
#include <QObject>


#include "AnMainwin.h"
#include "AnCallback.h"
#include "AnAdbchoseDialog.h"
#include "AnAdbServer.h"
#include "_AdbCheckCallback.h"


/*
class _AdbCheckCallback : public AnCallback
{
    Q_OBJECT
private:
    AnMainwin * anMainwin;
    AnAdbchoseDialog * dia;
public:
    _AdbCheckCallback(AnMainwin * amw) {
        anMainwin = amw;
        dia = new AnAdbchoseDialog(anMainwin);
        QObject::connect(dia, SIGNAL(sendYesCheck(QString)), this, SLOT(adb_file_chose(QString)));
    }

    void response(QJsonObject an_response) {
        std::cout << "response" << std::endl;
        switch (an_response.value(DATA).toObject().value(RESULT).toInt(-1)) {
        case 1:
            adb_installed = YES;
            emit adb_seted();
            break;
        case 0:
            adb_installed = NO;
            dia->show();
            break;
        default:
            dia->show();
            break;
        }

    }

public slots:
    void adb_file_chose(QString path)
    {
        adb_path = path;
        QJsonObject resq;
        resq.insert(NAME, QJsonValue("adb_check"));
        adb_server->adbServerManager(resq, this);
    }

signals:
    void adb_seted();

};

*/
/*
_AdbCheckCallback::_AdbCheckCallback(AnMainwin * amw) {
    anMainwin = amw;
    dia = new AnAdbchoseDialog(anMainwin);
    QObject::connect(dia, SIGNAL(sendYesCheck(QString)), this, SLOT(adb_file_chose(QString)));
}

void _AdbCheckCallback::response(QJsonObject an_response) {
    std::cout << "response" << std::endl;
    switch (an_response.value(DATA).toObject().value(RESULT).toInt(-1)) {
    case 1:
        adb_installed = YES;
        emit adb_seted();
        break;
    case 0:
        adb_installed = NO;
        dia->show();
        break;
    default:
        dia->show();
        break;
    }

}


void _AdbCheckCallback::adb_file_chose(QString path)
{
    adb_path = path;
    QJsonObject resq;
    resq.insert(NAME, QJsonValue("adb_check"));
    adb_server->adbServerManager(resq, this);
}

*/
class _DevicesRefreshCallback : public AnCallback
{
private:
    AnMainwin * anMainwin;
public:

    _DevicesRefreshCallback(AnMainwin  * amw)
    {
        anMainwin = amw;
    }

    void response(QJsonObject an_response)
    {
        AnGlobal::log_show(an_response, DESP_RESP);
        int result = an_response.value(DATA).toObject()
                .value(RESULT).toInt();

        QString id;
        QString model;

        if (result) {
            //anMainwin->mw->label_connect_status->setText("设备未连接");
            anMainwin->mw->comboBox_devices_list->clear();
            QJsonArray devices_list = an_response.value(DATA).toObject()
                    .value("devices").toArray();

            for (int t = 0; t < devices_list.size(); t ++) {
                QJsonObject each_device = devices_list.at(t).toObject();
                id = each_device.value("id").toString();
                model = each_device.value("model").toString();
                anMainwin->mw->comboBox_devices_list->addItem(model, QVariant(id));
            }

        } else {
            anMainwin->mw->label_connect_status->setText("设备刷新失败");
        }
    }
};



class _ConnectCloseCallback : public AnCallback
{
private:
    AnMainwin * amw;
public:
    _ConnectCloseCallback(AnMainwin * anMainwin)
    {
        amw = anMainwin;
    }
    void response(QJsonObject an_resp)
    {
        AnGlobal::log_show(an_resp, DESP_RESP);
        amw->mw->label_connect_status->setText("未连接");
        amw->mw->pushButton_connect->setText("连接");

    }
};


class _ConnectBuildCallback : public AnCallback
{
private:
    AnMainwin * amw;
public:

    _ConnectBuildCallback(AnMainwin * anMainwin)
    {
        amw = anMainwin;
    }

    void response(QJsonObject an_resp)
    {
        AnGlobal::log_show(an_resp, DESP_RESP);
        if (an_resp.value(DATA).toObject().value(RESULT).toInt()) {
            amw->mw->label_connect_status->setText("已连接到设备");
            amw->mw->pushButton_connect->setText("断开连接");
        } else {
            amw->mw->label_connect_status->setText("连接设备失败");
        }

    }
};



AnMainwin::AnMainwin()
{

    mw = new Ui::mainwin();
    mp = new Ui::mainpage();
    ap = new Ui::apppage();
    fp = new Ui::filepage();
    cp = new Ui::contactpage();

    _adb_check_cb = (void *) new _AdbCheckCallback(this);
    QObject::connect((_AdbCheckCallback *)_adb_check_cb, SIGNAL(adb_seted()), (AnMainwin *)this, SLOT(adbSeted()));
    _devices_refresh_cb = (void *) new _DevicesRefreshCallback(this);
    _connect_close_cb = (void *) new _ConnectCloseCallback(this);
    _connect_build_cb = (void *) new _ConnectBuildCallback(this);


    QObject::connect(sys_callback, SIGNAL(connect_accident()), this, SLOT(connect_accident_close()));
    QObject::connect(sys_callback, SIGNAL(connect_close()), this, SLOT(connect_close()));

    mw->setupUi(this);

    mp->setupUi(mw->tab_mainpage);
    ap->setupUi(mw->tab_app);
    fp->setupUi(mw->tab_file);
    cp->setupUi(mw->tab_contact);

    setNotConnect();

    setMainwin();
    setMainpage();
    setFilepage();
    setApppage();
    setContactpage();
}


AnMainwin::~AnMainwin() {
    delete mw;
    delete mp;
    delete ap;
    delete fp;
    delete cp;
}


void AnMainwin::setNotConnect() {
    mw->label_connect_status->setText("未连接");
    mw->pushButton_connect->setText("连接");
    mw->comboBox_devices_list->clear();

    mp->label_kernel->setText("未连接");
    mp->label_model->setText("未连接");
    mp->label_manuf->setText("未连接");
    mp->label_sysversion->setText("未连接");

    fp->label_file_path->setText(CUR_PATH);

}


void AnMainwin::start() {
    adb_check();

}


void AnMainwin::adb_check() {
    /*
    AnAdbchoseDialog *dia = new AnAdbchoseDialog(this);
    dia->setWindowModality(Qt::ApplicationModal);
    dia->show();
    */

    //QJsonObject

    QJsonObject adb_check_req;
    adb_check_req.insert(NAME, QJsonValue("adb_check"));
    adb_check_req.insert(STATUS, QJsonValue(REQUEST));

    adb_server->adbServerManager(adb_check_req, (_AdbCheckCallback *) _adb_check_cb);
    AnGlobal::log_show(adb_check_req, DESP_REQS);

}


void AnMainwin::adbSeted()
{
    devices_refresh();

}


void AnMainwin::devices_refresh()
{
    QJsonObject reqs;
    reqs.insert(NAME, QJsonValue("devices_check"));
    reqs.insert(STATUS, QJsonValue(REQUEST));

    adb_server->adbServerManager(reqs, (_DevicesRefreshCallback *) _devices_refresh_cb);
    AnGlobal::log_show(reqs, DESP_REQS);
}


void AnMainwin::setMainwin()
{
    QObject::connect(mw->pushButton_connect, SIGNAL(clicked()),
                     this, SLOT(pushbutton_connect_clicked()));
    QObject::connect(mw->pushButton_devices_refresh, SIGNAL(clicked()),
                     this, SLOT(pushbutton_refresh_clicked()));
}


void AnMainwin::pushbutton_connect_clicked()
{
    if (AnGlobal::is_connecting()) {

        if (!AnGlobal::is_request_processing("connect_close"))
        {
            QJsonObject reqs;
            reqs.insert(NAME, QJsonValue("connect_close"));
            reqs.insert(STATUS, QJsonValue(REQUEST));
            adb_server->adbServerManager(reqs, (_ConnectCloseCallback *)_connect_close_cb);
            AnGlobal::log_show(reqs, DESP_REQS);
        }

    } else {
        if (!AnGlobal::is_request_processing("connect_build")) {
            QVariant combox_cur = mw->comboBox_devices_list->currentData();
            if (combox_cur.isValid()) {
                QString dev_id = combox_cur.toString();
                QJsonObject reqs;
                QJsonObject reqs_data;
                reqs.insert(NAME,  QJsonValue("connect_build"));
                reqs.insert(STATUS, QJsonValue(REQUEST));
                reqs_data.insert("id", QJsonValue(dev_id));
                reqs.insert(DATA, QJsonValue(reqs_data));
                adb_server->adbServerManager(reqs, (_ConnectBuildCallback *)_connect_build_cb);
                AnGlobal::log_show(reqs, DESP_REQS);
            }
        }
    }
}


void AnMainwin::pushbutton_refresh_clicked()
{
    if (!AnGlobal::is_request_processing("devices_check")) {
        devices_refresh();
    }
}


void AnMainwin::connect_accident_close()
{
    mw->label_connect_status->setText("连接意外中断");
    mw->pushButton_connect->setText("连接");
    //devices_refresh();
}

void AnMainwin::connect_close()
{
    pushbutton_refresh_clicked();
}
